package mpicbg.imagefeatures;


import java.io.Serializable;
import java.util.ArrayList;

/**
 * SIFT feature container
 */
public class Feature implements Comparable< Feature >, Serializable
{
	public float scale;
	public float orientation;
	public float[] location;
	public float[] descriptor;

	/** Dummy constructor for Serialization to work properly. */
	public Feature() {}
	
	
	
	public Feature( float s, float o, float[] l, float[] d )
	{
		scale = s;
		orientation = o;
		location = l;
		descriptor = d;
	}
	public Feature( float[] perfeature )
	{
		scale = perfeature[0];
		orientation = perfeature[1];
		location = new float[2];
		location[0] = perfeature[2];
		location[1] = perfeature[3];
		descriptor = new float[perfeature.length - 4];
		for(int i = 0; i < descriptor.length; i++)
			descriptor[i] = perfeature[i + 4];
	}
	

	public String toString1()
	{
		String s = " ";
		String d = " ";

		for(int i=0;i<location.length;i++)
			s+=location[i]+" ";
		for(int i=0;i<descriptor.length;i++)
			d+=descriptor[i]+" ";
		return "scale:"+scale+"\n"+"orientation:"+orientation+"\n"+"location:"+s+"\n"+"descriptor:"+d+"\n";
	}
	public Float[] toArraylist()
	{
		ArrayList<Float> allarrays = new ArrayList<Float>();
		Float[] allfloats =new Float[129];
		ArrayList<Float> desc = new ArrayList<Float>();
		allarrays.add(scale);
		for(int i=0;i<descriptor.length;i++)
			desc.add(descriptor[i]);
		allarrays.addAll(desc);
		//System.out.println("the "+allarrays);
		for(int c=0;c<129;c++)
		{
			allfloats[c]=allarrays.get(c);
			//System.out.println("  "+allfloats[c]);
		}
		return allfloats;

	}
	/**
	 *  Converts from the Feature data to all floats
	 *  
	 *  @return floats
	 */
	public Float[] toFloats()
	{
		ArrayList<Float> allarrays = new ArrayList<Float>();
		ArrayList<Float> desc = new ArrayList<Float>();
		ArrayList<Float> loc = new ArrayList<Float>();
		
		allarrays.add(scale);
		allarrays.add(orientation);
		for(int i=0;i<location.length;i++)
			loc.add(location[i]);
		allarrays.addAll(loc);
		for(int i=0;i<descriptor.length;i++)
			desc.add(descriptor[i]);
		allarrays.addAll(desc);
		Float[] allfloats =new Float[allarrays.size()];
		//System.out.println("the "+allarrays);
		for(int c = 0 ; c < allarrays.size() ; c++)
		{
			allfloats[c] = allarrays.get(c);
			//System.out.println("  "+allfloats[c]);
		}
		return allfloats;
	}
	

	/**
	 * comparator for making Features sortable
	 * please note, that the comparator returns -1 for
	 * this.scale &gt; o.scale, to sort the features in a descending order  
	 */
	public int compareTo( Feature f )
	{
		return scale < f.scale ? 1 : scale == f.scale ? 0 : -1;
	}
	
	public float descriptorDistance( Feature f )
	{
		float d = 0;
		for ( int i = 0; i < descriptor.length; ++i )
		{
			float a = descriptor[ i ] - f.descriptor[ i ];
			d += a * a;
		}
		return ( float )Math.sqrt( d );
	}
}

